import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/bean/list_app_article_basic_entity.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/wx_text.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';

import '../../../r.dart';
import 'search_logic.dart';

/**
 * @Description: 搜索
 * @Author: 下雨了
 * @CreateDate: 2022-6-23 13:47:07
 * @Version: 1.0
 */
///
class SearchPage extends StatefulWidget {
  const SearchPage({Key? key}) : super(key: key);

  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  final logic = Get.put(SearchLogic());
  final state = Get.find<SearchLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SearchLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: MyColors.FFF1F1F1,
        body: GestureDetector(
          child: Stack(
            children: [
              Image.asset(R.assetsImgBg112, fit: BoxFit.fitWidth),
              Column(
                children: [
                  Container(
                    margin: EdgeInsets.only(top: 12.r),
                    child: AppBar(
                      backgroundColor: Colors.transparent,
                      elevation: 0,
                      title: Container(
                        height: 44.r,
                        width: Get.width / 6 * 4,
                        padding: EdgeInsets.only(top: 1.r, bottom: 1.r, left: 1.r, right: 1.r),
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(4),
                            color: MyColors.FFFFFFFF),
                        child: BrnSearchText(
                          controller: logic.textController,
                          searchController: logic.searchController..isActionShow = true,
                          hintText: "输入关键字进行搜索",
                          hintStyle: TextStyle(color: MyColors.FF999999, fontSize: 13.sp,),
                          textStyle: TextStyle(color: MyColors.FF333333, fontSize: 14.sp,),
                          innerPadding: EdgeInsets.all(5.r),
                          innerColor: MyColors.FFFFFFFF,
                          borderRadius: BorderRadius.circular(4),
                          maxHeight: 42.0.r,
                          prefixIcon: Center(),
                          action: InkWell(
                            child: SizedBox(
                              width: 42,
                              height: 42,
                              child: Center(
                                child: Image.asset(R.assetsImgIcon60, width: 16.r,),
                              ),
                            ),
                            onTap: () {
                              logic.searchArticle(logic.textController.text);
                            },
                          ),
                          textInputAction: TextInputAction.search,
                          onTextCommit: (String content) {
                            logic.searchArticle(content);
                          },
                        ),
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 16.r, right: 16.r),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Expanded(
                              child: Text('搜索历史', style: TextStyle(color: Colors.white, fontSize: 14.sp),),
                            ),
                            IconButton(
                              onPressed: () {
                                logic.deleteRecord();
                              },
                              icon: Icon(Icons.delete, color: Colors.white, size: 20.r),
                            ),
                          ],
                        ),
                        Wrap(
                          spacing: 8.r,
                          runSpacing: 8.r,
                          children: logic.state.historyList.map((e) => InkWell(
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 11.r, vertical: 4.r),
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(15.r)),
                              // child: Text(e, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(color: Color(0xff333333), fontSize: 14.sp),),
                              child: WXText(e, maxLines: 1, wxOverflow: WXTextOverflow.ellipsisMiddle, style: TextStyle(color: Color(0xff333333), fontSize: 14.sp),),
                            ),
                            onTap: () {
                              logic.searchArticle(e, add: false);
                            },
                          ),).toList(),
                        ),
                      ],
                    ),
                  ),
                  SingleChildScrollView(
                    child: Container(
                      margin: EdgeInsets.only(top: 16.r, left: 16.r, right: 16.r, bottom: 16.r),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5.r),
                        color: MyColors.FFFFFFFF
                      ),
                      child: ListView.separated(
                        padding: EdgeInsets.only(top: 0),
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: state.listAppArticleBasicEntity?.content?.length ?? 0,
                        itemBuilder: (BuildContext context, int index) {
                          return ListItem(index, state.listAppArticleBasicEntity?.content?[index]);
                        },
                        separatorBuilder: (BuildContext context, int index) {
                          return Container(height: 0.5.r, color: MyColors.FFDDDDDD,);
                        },
                      ),
                    ),
                  )
                ],
              ),
            ],
          ),
          onTap: () {
            // 空白处取消TextField焦点
            FocusScopeNode currentFocus = FocusScope.of(context);
            if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
              FocusManager.instance.primaryFocus!.unfocus();
            }
          },
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<SearchLogic>();
    super.dispose();
  }

  Widget ListItem(int index, ListAppArticleBasicContent? item) {
    return Container(
      margin: EdgeInsets.only(left: 8.r, right: 8.r, top: 8.r, bottom: 8.r),
      child: InkWell(
        child: Row(
          children: [
            Container(
              margin: EdgeInsets.only(right: 16.r),
              alignment: Alignment.center,
              width: 18.r, height: 18.r,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(2.r),
                color: MyColors.FFF1F1F1,
              ),
              child: Text((index + 1).toString(), style: TextStyle(color: MyColors.FF333333, fontSize: 14.sp),),
            ),
            Flexible(
              child: Text(
                item?.title ?? "",
                maxLines: 1,
                style: TextStyle(color: MyColors.FF333333, fontSize: 16.sp),
              ),
            )
          ],
        ),
        onTap: () {
          logic.goToNewsDetailPage(item?.id);
        },
      ),
    );
  }
}
